<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    // 后台登录页
    public function index(){
        if(isset($_SESSION['admin'])){
            $url = 'http://localhost/phone/index.php/Admin/Index/manage';
            header("location: $url");
        }else{
            $this->display();
        }
    }
    // 登录函数
    public function login($name, $password){
        // $name = $_POST['name'];
        // $password = $_POST['password'];
        $admin = M('admin');
        $data = $admin->where("name='$name' AND password='$password'")->find();
        if($data){
            $_SESSION['admin']=$name;
            $result = array(
                'code' => '0',
                'ext' => '登录成功',
                'adminName' => $_SESSION['admin']
            );
            echo json_encode($result);
        }else{
            $result = array(
                'code' => '1',
                'ext' => '用户名或密码错误'
            );
            echo json_encode($result);
        }
    }

    // 管理页面首页
    public function manage(){
        if(!isset($_SESSION['admin']) || !$_SESSION['admin']){
            $url = 'http://localhost/phone/index.php/Admin/Index';
            header("location: $url");
        }else{
            // 刷列表
            $phone = D('phone');
            $phoneList = $phone->order('create_at desc,id desc')->select();
            $this->assign('phoneList',$phoneList);

            $message = D('message');
            $messageList = $message->order('id desc')->select();
            $this->assign('messageList',$messageList);

            //刷订单
            $order = D('order'); 
            $user = D('user');     
            $orderList = $order->order('create_at desc')->select();        
            for ($i=0; $i < count($orderList); $i++) { 
            $userId = $order->field('user_id')->order('create_at desc')->select();
            $userid = $userId[$i]['user_id'];
            $username = $user->field('username')->where("id='$userid'")->select(); 
            $orderList[$i]['username'] = $username[0]['username'];                     
                    $itemList = $orderList[$i]['orderitemlist'];
                    $orderItemList[$i]=explode(',',$itemList);
                    //dump(count($orderItemList[$i]));
                    $orderItem = D('orderitem');
                    $phone = D('phone');                 
                    for ($j = 0; $j < count($orderItemList[$i]); $j++) {                                                     
                            $orderItemId[$i] = $orderItemList[$i][$j];
                            $s[$i][$j] = $orderItem->where("id='$orderItemId[$i]'")->select();
                            $s1[$i][$j] = $s[$i][$j][0];
                            $phoneId = $orderItem->field('phone_id')->where("id='$orderItemId[$i]'")->select();
                            $phoneid = $phoneId[0]['phone_id'];
                            $phoneName = $phone->field('name')->where("id='$phoneid'")->select();
                            // $number = $phone->field('number')->where("id='$phoneid'")->select();
                            // $s1[$i][$j]['number'] = $number[0]['number'];                        
                            $s1[$i][$j]['phonename'] = $phoneName[0]['name'];                                                                           
                    }                  
                $orderList[$i]['item'] = $s1[$i];              
                        
            }   
            $this->assign('orderList',$orderList); 
            $this->display();
        }
    }

    // 商品添加
    public function phoneAdd(){
        $name = $_POST['name'];
        $img = $_POST['img'];       
        $info = $_POST['info'];
        $productdate = $_POST['productdate'];
        $count = $_POST['count'];
        $type = $_POST['type'];
        $price = $_POST['price'];
        $discountprice = $_POST['discountprice'];
        if(!$name || !$img || !$productdate || !$info || !$count ||!$price|| !$discountprice){
            $result = array(
                'code'=> '1',
                'ext'=> '缺少参数'
            );
            echo json_encode($result,JSON_UNESCAPED_UNICODE);
            return;
        }
        $phone = D('phone');
        $data['name'] = $name;
        $data['img'] = $img;
        $data['info'] = $info;
        $data['productdate'] = $productdate;
        $data['count'] = $count;
        $data['type'] = $type;
        $data['price'] = $price;
        $data['discountprice'] = $discountprice;
        $data['create_at'] = date('Y-m-d H:i:s');
        $data['update_at'] = $data['create_at'];
        if($phone->create($data)){
            $id = $phone->add();
            if($id){
                $phone_template = $phone->where("id='$id'")->find();
                $result = array(
                    'code' => '0',
                    'ext' => 'success',
                    'obj' => $phone_template
                );
                //echo json_encode($result,JSON_UNESCAPED_UNICODE);
                echo json_encode($result);
            }
        }
    }

 
    //商品删除
    public function phoneDelete(){
        $id = $_POST['id'];
        $phone = D('phone');
        $data['state'] = 1;        
        //$success = $project->where("id='$id'")->delete();
        $success = $phone->where("id='$id'")->delete($data);
        if($success){
            $result = array(
                'code'=> '0',
                'ext'=> 'success'
            );
            echo json_encode($result);
        }else {
            $result = array(
                'code'=> '1',
                'ext'=> 'fail'
            );
            echo json_encode($result);
        }
    }
    //刷列表
    public function phoneList(){
        $phone = D('phone');
        $resultArr = $phone->select();
        //echo json_encode($resultArr,JSON_UNESCAPED_UNICODE);
        echo json_encode($result);
    }
    //商品编辑
    public function phoneEdit($id,$name,$img,$info,$productdate,$discountprice,$count,$type,$price){
        $phone = D('phone');
        $data['name'] = $name;
        $data['img'] = $img;
        $data['info'] = $info;
        $data['productdate'] = $productdate;
        $data['count'] = $count;
        $data['type'] = $type;
        $data['price'] = $price;
        $data['discountprice'] = $discountprice;
        $data['create_at'] = date('Y-m-d H:i:s');
        $data['update_at'] = $data['create_at'];
        $success = $phone->where("id='$id'")->save($data);
        if($success){
            $phone_template = $phone->where("id='$id'")->find();
            $result = array(
                'code'=> '0',
                'ext'=> 'success',
                'obj'=>$phone_template
            );
            echo json_encode($result);
        }else {
            $result = array(
                'code'=> '1',
                'ext'=> 'fail'
            );
            echo json_encode($result);
        } 
    } 
    
    //删除留言
    public function messageDelete(){
        $id = $_POST['id'];
        $message = D('message');
        $data['state'] = 1;        
        //$success = $project->where("id='$id'")->delete();
        $success = $message->where("id='$id'")->delete($data);
        if($success){
            $result = array(
                'code'=> '0',
                'ext'=> 'success'
            );
            echo json_encode($result);
        }else {
            $result = array(
                'code'=> '1',
                'ext'=> 'fail'
            );
            echo json_encode($result);
        }
    }
    //刷列表
    public function messageList(){
        $message = D('message');
        $resultArr = $message->select();
        //echo json_encode($resultArr,JSON_UNESCAPED_UNICODE);
        echo json_encode($result);
    }   
}
